package com.neusoft.work.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.work.dao.UserDao;
import com.neusoft.work.po.PageResponseBean;
import com.neusoft.work.po.ResponseBean;
import com.neusoft.work.po.User;
import com.neusoft.work.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao ud;

	@Override
	public ResponseBean<Integer> addUser(User user) {
		// TODO Auto-generated method stub
		int count = ud.insert(user);
		if (count > 0) {
			return new ResponseBean<>(user.getId());
		} else {
			return new ResponseBean<>(500, "添加失败");
		}
	}

	@Override
	public ResponseBean<Integer> updateUser(User user) {
		// TODO Auto-generated method stub
        int count = ud.updateById(user);
        if (count > 0) {
            return new ResponseBean<>(count);
        } else {
            return new ResponseBean<>(500, "更新失败");
        }
	}

	@Override
	public ResponseBean<Integer> deleteUser(Integer id) {
		// TODO Auto-generated method stub
        // 逻辑删除：设置 isDeleted 字段为 true
        User user = new User();
        user.setId(id);
        user.setIsDeleted(true);
        int count = ud.updateById(user);
        if (count > 0) {
            return new ResponseBean<>(count);
        } else {
            return new ResponseBean<>(500, "删除失败");
        }
	}

	@Override
	public PageResponseBean<List<User>> queryUserByName(String realName,Integer role,Long cur,Long number) {
		// TODO Auto-generated method stub
	    Page<User> page = new Page<>(cur, number);
	    QueryWrapper<User> qw = new QueryWrapper<>();
	    // realName 非空时才添加模糊查询
	    if (realName != null && !realName.trim().isEmpty()) {
	        qw.like("realName", realName.trim());
	    }
	    
	    if(role!=null) {
	    	qw.eq("role", role);
	    }
	    // 逻辑未删除
	    qw.eq("isDeleted", false);

	    // 3. 执行分页查询
	    IPage<User> userPage = ud.selectPage(page, qw);
	    List<User> list = userPage.getRecords();
		long total=userPage.getTotal();
		PageResponseBean<List<User>> rb=null;
	    // 4. 返回结果
	    if (total>0) {
	    	rb=new PageResponseBean<>(list);
	    	rb.setTotal(total);
	    } else {
	    	rb=new PageResponseBean<>(500,"no data");
	    }
	    return rb;
	}

	@Override
	public ResponseBean<User> getUser(User user) {
		// TODO Auto-generated method stub
		QueryWrapper<User> qw = new QueryWrapper<>();
		qw.eq("userName", user.getUserName());
		User result = ud.selectOne(qw);
		ResponseBean<User> rb = null;
		System.out.println(result);
		if (result == null) {
			rb = new ResponseBean<>(500, "账号错误");
		} else if (!user.getPassword().equals(result.getPassword())) {
			rb = new ResponseBean<>(500, "密码错误");
		} else {
//			向前端传输前，密码应该置空
			result.setPassword(null);
			rb = new ResponseBean<>(result);
		}
		return rb;
	}

}
